<?php
$path = __DIR__."/../../../";
require_once($path.'_inc/app.php');
$dBpages = Db::getModel("pages");
$time_start = microtime(1);

	$book_arr = $dBpages->db->getRows("SELECT MAX(book_id) AS id FROM pages");
	$book_id = ($book_arr[0]['id'])?:1;
	$page_arr = $dBpages->db->getRows("SELECT MAX(num_page) AS id FROM pages WHERE book_id =".intval($book_id));
	$page_id = $page_arr[0]['id']+1;
	

	$data = grubber2("http://www.loveread.ec/read_book.php?id=".$book_id."&p=".$page_id);
//_d($data);
	if($data){
		preg_match("/<span class='current'>(\d*)/", $data ,$page);
		$page = $page[1];
		if($page != $page_id){
			$book_id++;
			$page_id = 1;
			$data = grubber2("http://www.loveread.ec/read_book.php?id=".$book_id."&p=".$page_id);
		}

		preg_match("/<div class=\"MsoNormal\" style=\"margin:15px; text-align:left; width:800px; color:#393939;\">(.*)<\/p> <\/div> <div class='navigation' align='center' style='margin-bottom:20px;'>/", $data ,$text);
		$text = str_replace(array('<p class="MsoNormal">',"<p class=MsoNormal>"), '<p class="paragraph">', $text[1]); 
		$text = preg_replace('/<img(?:\\s[^<>]*)?>/i', '', $text);
		$text = strip_tags($text, '<p><div><ul><li>');
		
		$dBpages->insert(array('book_id'=>$book_id,"num_page"=>$page_id,"text"=>$text));
	}

echo  microtime(1) - $time_start;